package com.woniu108.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.order.model.dto.OrderRefundDto;
import com.woniu108.order.model.form.OrderProductRefundApplyForm;
import com.woniu108.order.model.form.OrderRefundAgreeRefundForm;
import com.woniu108.order.model.form.OrderRefundAgreeRefundParam;
import com.woniu108.order.model.form.OrderRefundDisagreeForm;
import com.woniu108.order.model.model.OrderRefund;
import com.woniu108.order.model.param.OrderProductRefundApplyParam;
import com.woniu108.order.model.param.OrderRefundDisagreeParam;
import com.woniu108.order.service.OrderRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单退款申请表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/orderRefund")
@Api(tags = "订单退款记录接口")
public class OrderRefundController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OrderRefundService orderRefundService;
    /**
     * token获取用户id
     * @param token 会话跟踪
     * @return 用户id
     */
    public String getUserIdByToken(String token){
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseUserToken(refreshToken).get("userId").toString();
    }

    /**
     * 申请退款(用户)
     * @param form 订单编号 退款原因 申请退款时间
     * @param token 登录userId
     * @return boolean
     */
    @PostMapping("/applyRefund")
    @ApiOperation("申请退款(用户)")
    public Result<Boolean> applyRefund(@RequestBody OrderProductRefundApplyForm form, @RequestHeader String token){
        String userId = getUserIdByToken(token);

        OrderProductRefundApplyParam param = BeanUtil.toBean(form, OrderProductRefundApplyParam.class);
        param.setUserId(userId);
        Boolean aBoolean=orderRefundService.insertOrderRefund(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("申请退款成功");
    }

    /**
     * 同意退款(管理员)
     * @param form 申请退款订单编号 同意退款金额 处理时间
     * @return boolean
     */
    @PostMapping("/agreeRefund.api")
    @ApiOperation("同意退款(管理员)")
    @AdminHistoryLog
    public Result<Boolean> agreeRefund(@RequestBody OrderRefundAgreeRefundForm form) throws AlipayApiException {
        OrderRefundAgreeRefundParam param = BeanUtil.toBean(form, OrderRefundAgreeRefundParam.class);

        Boolean bool=orderRefundService.agree(param);
        if(bool){
            return Result.getResult(Boolean.class).setData(bool).setMsg("退款成功");
        }else {
            return Result.getResult(Boolean.class).setData(false).setCode(1000).setMsg("退款失败");
        }
    }

    /**
     * 取消退款申请(用户)
     * @param orderProductNum 商品订单编号
     * @return 判定值
     */
    @GetMapping("/cancelRefund/{orderProductNum}")
    @ApiOperation("取消退款申请(用户)")
    public Result<Boolean> cancelRefund(@PathVariable("orderProductNum")@ApiParam("商品订单编号")String orderProductNum,@RequestHeader String token){
        String userId = getUserIdByToken(token);
        Boolean aBoolean=orderRefundService.cancel(orderProductNum,userId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("退款成功");
    }

    /**
     * 驳回申请（管理员）
     * @param form
     * @return 判定值
     */
    @PostMapping("/disagreeRefund.api")
    @ApiOperation("驳回申请（管理员）")
    @AdminHistoryLog
    public Result<Boolean> disagreeRefund(@RequestBody OrderRefundDisagreeForm form){
        OrderRefundDisagreeParam param = BeanUtil.toBean(form, OrderRefundDisagreeParam.class);
        Boolean aBoolean=orderRefundService.disagree(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("已驳回申请");
    }

    /**
     * 查询所有申请（管理员）
     * @return 退款申请表
     */
    @GetMapping("/queryAllRefund.api")
    @ApiOperation("查询所有申请（管理员），排除已处理申请")
    public Result<List<OrderRefundDto>> queryAllRefund(){
        List<OrderRefundDto> orderRefundDtos=orderRefundService.queryAllRefund();
        return Result.newListInstance(OrderRefundDto.class).setData(orderRefundDtos).setMsg("查询申请表成功");
    }

    /**
     * 查询订单的驳回信息
     * @param orderProductNum 商品订单信息
     * @return map 订单的驳回信息
     */
    @GetMapping("/queryRefundByOrderProductNum/{orderProductNum}.c")
    @ApiOperation("查询订单的驳回信息")
    public Result<Map<Object,Object>> queryRefund(@PathVariable("orderProductNum")@ApiParam("商品订单信息") String orderProductNum){
        Map<Object,Object> map=orderRefundService.selectRefundByOrderProductNum(orderProductNum);
        Result<Map<Object, Object>> result = new Result<>();
        if(map!=null){
            result.setData(map).setMsg("查询成功");
        }else {
            result.setData(null).setMsg("暂无信息");
        }
        return result;
    }
}

